﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.User
{
    public partial class EditUser : Base.BasePage
    {
        ExtendBLL.UserTable EBut = new ExtendBLL.UserTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("YHGL");
            strPageUserType.Add("mana");
            if (!IsPostBack)
            {
                if (Request.QueryString["UserId"] == null)
                {
                    Common.JShelper.JSAlert(this.Page, "", "你无权查看此页面！");
                }
                else
                {
                    int Userid = int.Parse(Request.QueryString["UserId"].ToString());
                    UserIDHF.Value = Userid.ToString();
                    InitBind(Userid);
                    InitRoleInfo(Userid);
                    tb.Style.Add("display", "none");
                    tbRealName.Attributes.Add("readOnly","true");
                }
            }
        }

        /// <summary>
        /// 初始化页面信息
        /// </summary>
        protected void InitBind(int Userid)
        {
            List<LabMS.Model.UserTable> listUT = new List<LabMS.Model.UserTable>();
            listUT = EBut.GetUserTableByUserID(Userid);
            if (listUT.Count > 0)
            {
                lblCreateTime.Text = listUT[0].CreateTime.Value.ToShortDateString();//创建时间
                tbUserName.Text = listUT[0].UserName;//用户名 
                UserNameHF.Value = listUT[0].UserName;
                string PCode= listUT[0].PCode;//老师代码
                tbRealName.Text = EBut.GetRealNameByPCode(PCode);//真实姓名
                tbPassword.Text = listUT[0].Password;//密码
            }
        }

        /// <summary>
        /// 初始化绑定角色信息
        /// </summary>
        /// <param name="UserId"></param>
        protected void InitRoleInfo(int UserId)
        {
            ExtendBLL.UserRole BUR = new ExtendBLL.UserRole();
            try
            {
                gvRole.DataSource = BUR.GetUserRoleByUserID(UserId);
                gvRole.DataBind();
            }
            catch(Exception ex)
            {
                if (ex.Message == "1")
                {
                    Common.JShelper.JSAlert(this.Page,"","该用户不存在！");
                    gvRole.DataSource = "";
                    gvRole.DataBind();
                }
                if (ex.Message == "2")
                {
                    Common.JShelper.JSAlert(this.Page,"","该用户还没有分配角色！");
                    gvRole.DataSource = "";
                    gvRole.DataBind();
                }
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            int UserId = int.Parse(Request.QueryString["UserId"].ToString());
            string UserName = tbUserName.Text.Trim();
            string Password = tbPassword.Text.Trim();
            string PCode = tb.Text.Trim();

            int count;
            count = EBut.UpdateUserTable(UserId, UserName, PCode, Password);//更新 
            if (count > 0)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>alert('修改用户信息成功！');window.location.href='UserDefault.aspx';</script>");
            }
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void tbAddRole_Click(object sender, EventArgs e)
        {
            int UserID;
            if(UserIDHF.Value!="")
            {
                if(int.TryParse(UserIDHF.Value,out UserID))
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>window.open('AddRole.aspx?UserId=" + UserID + "&UserName="+Server.UrlEncode(UserNameHF.Value)+"','','height=300,width=400');</script>");
                }
                else
                {
                    Common.JShelper.JSAlert(this.Page,"","用户ID存储不合法,请重新载入页面！");
                }
            }
            else
            {
                Common.JShelper.JSAlert(this.Page,"","用户ID存储失效,请重新载入页面！");
            }
            
        }

        /// <summary>
        /// gv生成事件时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvRole_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            ExtendBLL.UserRole EUR=new ExtendBLL.UserRole();
            int Userid = int.Parse(Request.QueryString["UserId"].ToString());

            if (e.CommandName == "cancleRole")
            {
                int id = int.Parse(e.CommandArgument.ToString());//当前行号
                int RoleID = id;
                try
                {
                    EUR.DeleteByRoleIDUserID(RoleID, Userid);
                    Common.JShelper.JSAlert(this.Page, "", "删除成功！");
                    InitRoleInfo(Userid);
                }
                catch(Exception ex)
                {
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 在数据绑定完成以后触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvRole_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'");
                e.Row.Attributes["style"] = "Cursor:pointer";
            }
        }

        /// <summary>
        /// 为模版列LinkButton写入CommandArgument事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvRole_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton linkb = (LinkButton)e.Row.FindControl("CancelRole");
                linkb.CommandArgument = e.Row.RowIndex.ToString();
            }
        }     
    }
}
